package me.jiatao.viewmodel

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import androidx.lifecycle.LiveData
import androidx.lifecycle.coroutineScope
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.liveData
import kotlinx.coroutines.delay


class MyFragment : Fragment() {

    private val liveIndex: LiveData<String> by lazy {
        liveData {
            for (index in 1..Int.MAX_VALUE) {
                delay(500)
                emit("current $index")
            }
        }
    }

    private lateinit var button: Button;

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_my, container, false)

        button = view.findViewById(R.id.fragment_button)
        button.setOnClickListener {
            onClick()
        }
        return view
    }

    private fun onClick() {
        if (lifecycleScope == viewLifecycleOwner.lifecycleScope) {
            Log.e("MyFragment", "11111111")
        } else {
            Log.e("MyFragment", "2222222222")
        }

//lifecycle
//        liveIndex.observe(viewLifecycleOwner, {
//            println(it)
//            button.text = it
//        })
    }
}